using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LocalDnsSdk.Commands
{
    // 非参数类指令通用：STATUS/START/STOP/RESTART/LIST_RECORDS/CLEAR_RECORDS/CLEAR_CACHE/
    // GET_CACHE_STATS/GET_CONFIG/SAVE_CONFIG/GET_STATISTICS/LIST_UPSTREAM/LIST_COMMANDS/HELP/EXIT

    public sealed class StatusCommand : BaseCommand
    {
        public const string NAME = "STATUS";
        public override string Name => NAME;
        public override string Format => "STATUS";
        public override string Description => "显示DNS服务状态";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class StartCommand : BaseCommand
    {
        public const string NAME = "START";
        public override string Name => NAME;
        public override string Format => "START";
        public override string Description => "启动DNS服务";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class StopCommand : BaseCommand
    {
        public const string NAME = "STOP";
        public override string Name => NAME;
        public override string Format => "STOP";
        public override string Description => "停止DNS服务";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class RestartCommand : BaseCommand
    {
        public const string NAME = "RESTART";
        public override string Name => NAME;
        public override string Format => "RESTART";
        public override string Description => "重启DNS服务";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }

    public sealed class AddCommand : BaseCommand
    {
        public const string NAME = "ADD";
        public override string Name => NAME;
        public override string Format => "ADD domain ip [ttl] [description]";
        public override string Description => "添加域名解析记录";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override IEnumerable<string> Aliases => new[] { "ADDRECORD", "ADD_RECORD" };
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            if (args.Length < 2) { error = "ADD 需要至少两个参数: domain ip"; return false; }
            if (!IPAddress.TryParse(args[1], out _)) { error = "ADD 的 ip 参数无效"; return false; }
            if (args.Length >= 3 && !int.TryParse(args[2], out _)) { error = "ADD 的 TTL 必须为整数"; return false; }
            return true;
        }
    }
    public sealed class AddPtrCommand : BaseCommand
    {
        public const string NAME = "ADD_PTR";
        public override string Name => NAME;
        public override string Format => "ADD_PTR ip hostname [ttl] [description]";
        public override string Description => "添加IP的PTR反向记录";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override IEnumerable<string> Aliases => new[] { "ADDPTRRECORD" };
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            if (args.Length < 2) { error = "ADD_PTR 需要至少两个参数: ip hostname"; return false; }
            if (!IPAddress.TryParse(args[0], out _)) { error = "ADD_PTR 的 ip 参数无效"; return false; }
            if (args.Length >= 3 && !int.TryParse(args[2], out _)) { error = "ADD_PTR 的 TTL 必须为整数"; return false; }
            return true;
        }
    }
    public sealed class RemoveCommand : BaseCommand
    {
        public const string NAME = "REMOVE";
        public override string Name => NAME;
        public override string Format => "REMOVE domain";
        public override string Description => "删除域名解析记录";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override IEnumerable<string> Aliases => new[] { "REMOVERECORD", "REMOVE_RECORD" };
        public override bool Validate(string[] args, out string error)
        { error = string.Empty; if (args.Length < 1) { error = "REMOVE 需要一个参数: domain"; return false; } return true; }
    }
    public sealed class ListRecordsCommand : BaseCommand
    {
        public const string NAME = "LIST_RECORDS";
        public override string Name => NAME;
        public override string Format => "LIST_RECORDS";
        public override string Description => "列出所有域名解析记录";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class RemovePtrCommand : BaseCommand
    {
        public const string NAME = "REMOVE_PTR";
        public override string Name => NAME;
        public override string Format => "REMOVE_PTR ip";
        public override string Description => "删除指定IP的PTR记录";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override IEnumerable<string> Aliases => new[] { "REMOVEPTRRECORD" };
        public override bool Validate(string[] args, out string error)
        { error = string.Empty; if (args.Length < 1) { error = "REMOVE_PTR 需要一个参数: ip"; return false; } if (!IPAddress.TryParse(args[0], out _)) { error = "REMOVE_PTR 的 ip 参数无效"; return false; } return true; }
    }
    public sealed class ClearRecordsCommand : BaseCommand
    {
        public const string NAME = "CLEAR_RECORDS";
        public override string Name => NAME;
        public override string Format => "CLEAR_RECORDS";
        public override string Description => "清空所有域名解析记录";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class ClearCacheCommand : BaseCommand
    {
        public const string NAME = "CLEAR_CACHE";
        public override string Name => NAME;
        public override string Format => "CLEAR_CACHE";
        public override string Description => "清空DNS缓存";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class GetCacheStatsCommand : BaseCommand
    {
        public const string NAME = "GET_CACHE_STATS";
        public override string Name => NAME;
        public override string Format => "GET_CACHE_STATS";
        public override string Description => "获取缓存统计信息";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class GetConfigCommand : BaseCommand
    {
        public const string NAME = "GET_CONFIG";
        public override string Name => NAME;
        public override string Format => "GET_CONFIG";
        public override string Description => "获取当前配置";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class SetConfigCommand : BaseCommand
    {
        public const string NAME = "SET_CONFIG";
        public override string Name => NAME;
        public override string Format => "SET_CONFIG key=value ...";
        public override string Description => "设置配置项";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            if (args.Length < 1) { error = "SET_CONFIG 至少需要一个 key=value"; return false; }
            if (args.Any(a => !a.Contains('='))) { error = "SET_CONFIG 参数需为 key=value 格式"; return false; }
            return true;
        }
    }
    public sealed class SaveConfigCommand : BaseCommand
    {
        public const string NAME = "SAVE_CONFIG";
        public override string Name => NAME;
        public override string Format => "SAVE_CONFIG";
        public override string Description => "保存配置到文件";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class GetStatisticsCommand : BaseCommand
    {
        public const string NAME = "GET_STATISTICS";
        public override string Name => NAME;
        public override string Format => "GET_STATISTICS";
        public override string Description => "获取服务统计";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class ResolveCommand : BaseCommand
    {
        public const string NAME = "RESOLVE";
        public override string Name => NAME;
        public override string Format => "RESOLVE domain";
        public override string Description => "测试解析指定域名";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error)
        { error = string.Empty; if (args.Length < 1) { error = "RESOLVE 需要一个参数: domain"; return false; } return true; }
    }
    public sealed class ListUpstreamCommand : BaseCommand
    {
        public const string NAME = "LIST_UPSTREAM";
        public override string Name => NAME;
        public override string Format => "LIST_UPSTREAM";
        public override string Description => "列出上游DNS信息";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class SetUpstreamMainDomainCommand : BaseCommand
    {
        public const string NAME = "SET_UPSTREAM_MAIN_DOMAIN";
        public override string Name => NAME;
        public override string Format => "SET_UPSTREAM_MAIN_DOMAIN ip main_domain";
        public override string Description => "设置指定上游DNS的主域名";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            if (args.Length < 2) { error = "SET_UPSTREAM_MAIN_DOMAIN 需要两个参数: ip main_domain"; return false; }
            if (!IPAddress.TryParse(args[0], out _)) { error = "SET_UPSTREAM_MAIN_DOMAIN 的 ip 参数无效"; return false; }
            if (string.IsNullOrWhiteSpace(args[1])) { error = "SET_UPSTREAM_MAIN_DOMAIN 的 main_domain 不能为空"; return false; }
            return true;
        }
    }
    public sealed class ListCommandsCommand : BaseCommand
    {
        public const string NAME = "LIST_COMMANDS";
        public override string Name => NAME;
        public override string Format => "LIST_COMMANDS";
        public override string Description => "以表格形式列出所有命令";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class HelpCommand : BaseCommand
    {
        public const string NAME = "HELP";
        public override string Name => NAME;
        public override string Format => "HELP";
        public override string Description => "显示帮助信息";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class ExitCommand : BaseCommand
    {
        public const string NAME = "EXIT";
        public override string Name => NAME;
        public override string Format => "EXIT";
        public override string Description => "退出连接";
        public override CommandPermission Permission => CommandPermission.User;
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
    public sealed class SetLogPolicyCommand : BaseCommand
    {
        public const string NAME = "SET_LOG_POLICY";
        public override string Name => NAME;
        public override string Format => "SET_LOG_POLICY SIZE|DAILY";
        public override string Description => "设置日志滚动策略";
        public override CommandPermission Permission => CommandPermission.Admin;
        public override IEnumerable<string> Aliases => new[] { "SET-LOG-POLICY" };
        public override bool Validate(string[] args, out string error)
        {
            error = string.Empty;
            if (args.Length != 1) { error = "用法: SET_LOG_POLICY SIZE|DAILY"; return false; }
            var v = (args[0] ?? "").Trim().ToUpperInvariant();
            if (v != "SIZE" && v != "DAILY") { error = "策略仅支持: SIZE 或 DAILY"; return false; }
            return true;
        }
    }

    public sealed class GetLogStatusCommand : BaseCommand
    {
        public const string NAME = "GET_LOG_STATUS";
        public override string Name => NAME;
        public override string Format => "GET_LOG_STATUS";
        public override string Description => "查看当前日志状态";
        public override CommandPermission Permission => CommandPermission.User;
        public override IEnumerable<string> Aliases => new[] { "LOG_STATUS" };
        public override bool Validate(string[] args, out string error) { error = string.Empty; return args.Length == 0 || (error = "此命令不接受参数") == string.Empty; }
    }
}